const Crypto = require('crypto');
const uuid  = require('uuid');

/**
 * 
 * @desc 生成指定范围[min, max]的随机数
 * @param  {Number} min 
 * @param  {Number} max 
 * @return {Number} 
 */
function num(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
 * 
 * @desc 从数组中随机抽取
 * @param  {Array} arr  
 * @return {???} 
 */
function numByArray(arr) {
    return arr[num(0, arr.length - 1)];
}


/**
 * 
 * @desc 生成随机变量名([A~Z],[a~z],[0~9])
 * @param  {Number} len  
 * @return {String} 
 */

function varName(len) {
    let s = "", t, ft = true;
    var ranNum = (l) => (Math.floor(Math.random() * (l[1] - l[0])) + l[0]);
    for (; len > 0; len--) {
        t = ranNum([[65, 91], [97, 123], [0, 10]][ranNum([0, ft ? 2 : 3])]);
        s = s + (t > 10 ? String.fromCharCode(t) : t.toString());
        if (ft) ft = false;
    }
    return s;
}

/**
 * @desc 打乱数组内部元素顺序
 * @param {Array} arr 
 */
function randomSort(arr) {
    return arr.sort(() => Math.random() - 0.5);
}


/**
 * @desc 生成UUID
 * @param  {String} s  
 * @return {String}
 */ 

function md5(a) {
    let md5 = Crypto.createHash('md5');
    return md5.update(a).digest('hex');
}

module.exports = {
    num, numByArray, varName, randomSort, uuidv1:uuid.v1,uuidv4:uuid.v4 ,md5
};